package com.claude.ui;

import com.special.ResideMenuDemo.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

public class SlideView extends LinearLayout {

	private Context mContext = null;
	private LinearLayout mViewContent = null;
	private Scroller mScroller = null;
	private OnSliderListener mOnSliderListener = null;

	private int mHolderWidth = 120;
	private int mLastX = 0;
	private int mLastY = 0;

	final int TAN = 2;

	public SlideView(Context context) {
		super(context);
		initView();
	}

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public interface OnSliderListener {
		public static final int SLIDE_STATUS_OFF = 0;
		public static final int SLIDE_STATUS_START_SCROLL = 1;
		public static final int SLIDE_STATUS_ON = 2;

		public void onSlider(View view, int status);
	}

	void initView() {
		mContext = getContext();
		mScroller = new Scroller(mContext);

		setOrientation(HORIZONTAL);
		View.inflate(mContext, R.layout.slide_view_merge, this);
		mViewContent = (LinearLayout) findViewById(R.id.view_content);
		mHolderWidth = Math.round(TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources()
						.getDisplayMetrics()));
	}

	public void setDeleteButton(CharSequence charsequence) {
		((TextView) findViewById(R.id.delete)).setText(charsequence);
	}

	public void setContentView(View view) {
		mViewContent.addView(view);
	}

	public void setOnSliderListener(OnSliderListener onSliderListener) {
		mOnSliderListener = onSliderListener;
	}

	/**
	 * scroll shrink
	 */
	public void shrink() {
		if (getScrollX() != 0) {
			smoothScrollTo(0, 0);
		}
	}

	/**
	 * scroll method
	 * 
	 * @param x
	 * @param y
	 */
	private void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int delta = destX - scrollX;
        mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
        invalidate();
    }

	public void onRequireTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int scrollX = getScrollX();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            if (mOnSliderListener != null) {
                mOnSliderListener.onSlider(this,
                        OnSliderListener.SLIDE_STATUS_START_SCROLL);
            }
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            int deltaX = x - mLastX;
            int deltaY = y - mLastY;
            if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) {
                break;
            }

            int newScrollX = scrollX - deltaX;
            if (deltaX != 0) {
                if (newScrollX < 0) {
                    newScrollX = 0;
                } else if (newScrollX > mHolderWidth) {
                    newScrollX = mHolderWidth;
                }
                this.scrollTo(newScrollX, 0);
            }
            break;
        }
        case MotionEvent.ACTION_UP: {
            int newScrollX = 0;
            if (scrollX - mHolderWidth * 0.75 > 0) {
                newScrollX = mHolderWidth;
            }
            this.smoothScrollTo(newScrollX, 0);
            if (mOnSliderListener != null) {
                mOnSliderListener.onSlider(this,
                        newScrollX == 0 ? OnSliderListener.SLIDE_STATUS_OFF
                                : OnSliderListener.SLIDE_STATUS_ON);
            }
            break;
        }
        default:
            break;
        }

        mLastX = x;
        mLastY = y;
    }

	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}
}
